<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>购物车</title>
	<base href="<%=request.getContextPath()%>/"/>
	<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <link href="css/guimei.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="../js/jquery-2.1.0.js"></script>
	<%@include file="layUI.jsp"%>
    <script type="text/javascript">
			//应付含运费
			var totalPrice='${totalPrice}';
			//优惠价格
			var concessionalRate='${concessionalRate}';
			$(function () {
				var flag='${param.flag}';
				if(flag=="true"){
					alert("添加成功！");
				}else if(flag=="false"){
					alert("均未购买成功");
				}
				showMoney();
				/* 通过ID修改购物车商品数目 */
				$(".number").on('input propertychange', function() {
					var dqshopNum=$(this);
					var shopNum=$(this).prev();	//得到最初的值
					var dhNumber=parseInt(dqshopNum.val());
					var shNumber=parseInt(shopNum.val());
					var slBianHuan=0;					//数量变化用于计算价格
					if(dqshopNum.val()>0){
						var youhui=parseInt($(this).parent().prev().prev().html());		//优惠单价
						var shopId=$(this).next().val();	//得到修改的id
						$.post("/ServletCustomer",{"action":"updateNumber","shopId":shopId,"number":dhNumber},callbackUpdateNumber,"text");
						function callbackUpdateNumber(data) {
							if(data=="true"){
								if(shNumber>dhNumber){
									slBianHuan=shNumber-dhNumber;
									concessionalRate=parseInt(concessionalRate)-youhui*slBianHuan;
									totalPrice=parseInt(totalPrice)-youhui*slBianHuan;
								}else if(shNumber<dhNumber){
									slBianHuan=dhNumber-shNumber;
									concessionalRate=parseInt(concessionalRate)+youhui*slBianHuan;
									totalPrice=parseInt(totalPrice)+youhui*slBianHuan;
								}
								shopNum.val(dhNumber);
								showMoney();
							}else {
								alert("修改失败！服务器失去连接！");
								dqshopNum.val(shNumber);
							}
						}
					}else {
						alert("商品数不能小于1！");
						shopNum.val(1);
						dqshopNum.val(1);
					}
				})
				//购物车提交
				$("#submit").click(function () {
					var addres=$("#cuAddress").val();
					var name=$("#cuName").val();
					var phone1=$("#cuPhone1").val();
					var phone2=$("#cuPhone2").val();
					var phone3=$("#cuPhone3").val();
					var phoneSj=$("#shouji").val();
					addres="姓名:"+name+";地址"+addres+";电话"+phone1+phone2+phone3+";手机:"+phoneSj;
					location.href="/ServletCustomer?action=shopSubmission&addres="+addres;
				});
			})
			/* 通过ID删除购物车项目 */
	      function deleteShop(s_id,tag) {
	      		if(confirm("您确定要删除吗？")){
					 var youhui=parseInt($(tag).prev().prev().prev().html());		//优惠
					 var yunfei=parseInt($(tag).next().val());	//运费
					 var number=parseInt($(tag).prev().children().next().val());	//数量
      				$.post("/ServletCustomer?action=shopDel","spId="+s_id,callbackDelshop,"text");
      				function callbackDelshop(data) {
						if(data=="true"){
							concessionalRate=parseInt(concessionalRate)-parseInt(youhui*number);
							totalPrice=(totalPrice-parseInt(yunfei))-parseInt(youhui*number);
							$(tag).parent().remove();
							alert("删除成功！");
							showMoney();
						}else {
							alert("删除失败！");
						}
					}
	      		}
      		}
      		//显示优惠价和
      		function showMoney() {
				$("#sheng_money").html(parseInt(concessionalRate).toFixed(2));
				$("#count_money").html(parseInt(totalPrice).toFixed(2));
			}
    </script>
    
</head>
<body>
<%@include file="head.jsp"%>
    <div id="wodeguc" >
		<div style="color: red">${ord}</div>
        <img src="images/img/images/mycart.gif">
        <label class="wudeguclabel">全场满100免运费</label>
        <label class="wudeguclabel">简易计算器</label>
    </div>

    <div id="quanren"  style="margin: auto;">
        <p id="wodeguc_2jibiaoti">确认商品价格与交易条件</p>
        <table border="0px" width="95%"style="margin:30px;">
	        <thead style="font-size: 16px;color:red;line-height:65px; text-align: center; ">
	        	<tr id="quanrentable_hang1">
	                <td style="width:36%;text-align:left;">商品名</td>
	                <td style="width:20%;">图片</td>
	                <td style="width:8%;">原价</td>
	                <td style="width:8%;">优惠价</td>
	                <td style="width:8%;">打折</td>
	                <td style="width:8%;">数量</td>
	                <td style="width:8%;">操作</td>
	            </tr>
        	</thead>
				<c:forEach items="${ps.list}" var="cus" varStatus="i">
			<tr >

				<td style="width:36%;text-align:left;">${cus.goodsName}</td>
				<td style="width:20%;"><a  href="goodsServlet?action=goodsLookByIg&id=${cus.goodsId}" >
					<img src="GoodsImage/${cus.goodsImage}" style="width: 80px;height: 80px" ></a></td>
				<td style="width:8%;">${cus.goodsMoney}</td>
				<td style="width:8%;">${cus.discountPrice}</td>
				<td style="width:8%;">${cus.discRate}</td>
				<td style="width:8%;" ><input type="hidden" value="${cus.scNumber}"><input class="number" type="number" value="${cus.scNumber}"><input type="hidden" value="${cus.id}"></td>
				<td style="width:8%;"  onclick="deleteShop(${cus.id},this)"><a class="layui-btn layui-btn-sm">删除</a></td>
				<input type="hidden" value="${cus.goodsCarriage}"/>
			</tr>
				</c:forEach>
			<tr>
				<td colspan="7">
					<jsp:include page="Page.jsp"/>
				</td>
			</tr>
			<tr>

				<td colspan="5">
					<p >优惠金额￥:<b id="sheng_money"></b></p>
				</td>
				<td>
					<p >应付金额￥:<b id="count_money"></b></p>
				</td>
				<td style="text-align: right;">
					<p><a href="BeforePage/GUIMEI/homepage.jsp">返回继续购买商品</a></p>
				</td>
			</tr>
        </table>

    </div>
	<div id="dizhi"  style="margin: auto;">
          <p id="dizhi_2jibiaoti" >补充您的邮件地址和联系人信息</p>
          <br/>
		<form action="" method="post">
            姓名：<input type="text" id="cuName" name="cusName" value="${sessionScope.customer.cusName}"><br>
            地址：<input type="text" id="cuAddress" name="orderseAddress" value="北京"><br>
            电话：<input type="text" id="cuPhone1" size="4" name="quhao" value="0731">-<input id="cuPhone2" type="text" size="8" name="phone" value="88858332">-<input id="cuPhone3" type="text" size="4" name="fenji" value="8888">
            （区号-电话号码-分机）<br>
            手机：<input id="shouji" type="text" size="11" name="telpone" value="15874073679"><br>
		<a id="submit"><img src="images/img/zhuce/submit.gif"></a>
		</form>
    </div>
<div style="margin: 100px 0px 0px 0px"><jsp:include page="foot.jsp"/></div>
</body>
</html>